package cn.iocoder.yudao.module.promotion.controller.admin.shortlinklog;

import cn.iocoder.yudao.module.promotion.controller.admin.shortlinklog.vo.*;
import cn.iocoder.yudao.module.promotion.convert.shortlinklog.ShortLinkLogConvert;
import cn.iocoder.yudao.module.promotion.dal.dataobject.shortlinklog.ShortLinkLogDO;
import cn.iocoder.yudao.module.promotion.service.shortlinklog.ShortLinkLogService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

@Tag(name = "管理后台 - 短链接访问记录")
@RestController
@RequestMapping("/promotion/short-link-log")
@Validated
public class ShortLinkLogController {

    @Resource
    private ShortLinkLogService shortLinkLogService;

    @PostMapping("/create")
    @Operation(summary = "创建短链接访问记录")
    @PreAuthorize("@ss.hasPermission('zfkj:short-link-log:create')")
    public CommonResult<Long> createShortLinkLog(@Valid @RequestBody ShortLinkLogCreateReqVO createReqVO) {
        return success(shortLinkLogService.createShortLinkLog(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新短链接访问记录")
    @PreAuthorize("@ss.hasPermission('zfkj:short-link-log:update')")
    public CommonResult<Boolean> updateShortLinkLog(@Valid @RequestBody ShortLinkLogUpdateReqVO updateReqVO) {
        shortLinkLogService.updateShortLinkLog(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除短链接访问记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('zfkj:short-link-log:delete')")
    public CommonResult<Boolean> deleteShortLinkLog(@RequestParam("id") Long id) {
        shortLinkLogService.deleteShortLinkLog(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得短链接访问记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('zfkj:short-link-log:query')")
    public CommonResult<ShortLinkLogRespVO> getShortLinkLog(@RequestParam("id") Long id) {
        ShortLinkLogDO shortLinkLog = shortLinkLogService.getShortLinkLog(id);
        return success(ShortLinkLogConvert.INSTANCE.convert(shortLinkLog));
    }

    @GetMapping("/list")
    @Operation(summary = "获得短链接访问记录列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('zfkj:short-link-log:query')")
    public CommonResult<List<ShortLinkLogRespVO>> getShortLinkLogList(@RequestParam("ids") Collection<Long> ids) {
        List<ShortLinkLogDO> list = shortLinkLogService.getShortLinkLogList(ids);
        return success(ShortLinkLogConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得短链接访问记录分页")
    @PreAuthorize("@ss.hasPermission('zfkj:short-link-log:query')")
    public CommonResult<PageResult<ShortLinkLogRespVO>> getShortLinkLogPage(@Valid ShortLinkLogPageReqVO pageVO) {
        PageResult<ShortLinkLogDO> pageResult = shortLinkLogService.getShortLinkLogPage(pageVO);
        return success(ShortLinkLogConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出短链接访问记录 Excel")
    @PreAuthorize("@ss.hasPermission('zfkj:short-link-log:export')")
    public void exportShortLinkLogExcel(@Valid ShortLinkLogExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<ShortLinkLogDO> list = shortLinkLogService.getShortLinkLogList(exportReqVO);
        // 导出 Excel
        List<ShortLinkLogExcelVO> datas = ShortLinkLogConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "短链接访问记录.xls", "数据", ShortLinkLogExcelVO.class, datas);
    }

}
